#include <iostream>
#include <memory>
#include <grpcpp/grpcpp.h>
#include "protos/service_rpc.grpc.pb.h"

int main(int argc, char* argv[]) {
    std::string server_address = "localhost:9003";
    
    // 创建gRPC通道
    auto channel = grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials());
    auto stub = service_rpc::ServiceRPC::NewStub(channel);
    
    std::cout << "========================================" << std::endl;
    std::cout << "  gRPC Client Test" << std::endl;
    std::cout << "  Server: " << server_address << std::endl;
    std::cout << "========================================" << std::endl;
    
    // 测试1: 健康检查
    std::cout << "\n[Test 1] Health Check..." << std::endl;
    {
        service_rpc::HealthCheckRequest request;
        service_rpc::HealthCheckResponse response;
        grpc::ClientContext context;
        
        grpc::Status status = stub->HealthCheck(&context, request, &response);
        
        if (status.ok()) {
            std::cout << "✓ Health Check Success!" << std::endl;
            std::cout << "  - Healthy: " << (response.healthy() ? "Yes" : "No") << std::endl;
            std::cout << "  - Service: " << response.service_name() << std::endl;
            std::cout << "  - Version: " << response.version() << std::endl;
            std::cout << "  - Uptime: " << response.uptime_seconds() << " seconds" << std::endl;
        } else {
            std::cout << "✗ Health Check Failed!" << std::endl;
            std::cout << "  Error: " << status.error_message() << std::endl;
            return 1;
        }
    }
    
    // 测试2: 服务调用 (/api/ping)
    std::cout << "\n[Test 2] Call Service (/api/ping)..." << std::endl;
    {
        service_rpc::ServiceRequest request;
        request.set_path("/api/ping");
        request.set_method("GET");
        request.set_request_id("test-request-001");
        
        service_rpc::ServiceResponse response;
        grpc::ClientContext context;
        
        grpc::Status status = stub->Call(&context, request, &response);
        
        if (status.ok()) {
            std::cout << "✓ Service Call Success!" << std::endl;
            std::cout << "  - HTTP Status: " << response.http_status() << std::endl;
            std::cout << "  - Error Code: " << response.error_code() << std::endl;
            std::cout << "  - Message: " << response.message() << std::endl;
            if (!response.body().empty()) {
                std::cout << "  - Body: " << response.body() << std::endl;
            }
        } else {
            std::cout << "✗ Service Call Failed!" << std::endl;
            std::cout << "  Error: " << status.error_message() << std::endl;
            return 1;
        }
    }
    
    std::cout << "\n========================================" << std::endl;
    std::cout << "✅ All tests passed!" << std::endl;
    std::cout << "========================================" << std::endl;
    
    return 0;
}
